import {Body, Controller, Get, Post, UseGuards} from '@nestjs/common';
import {ApiBearerAuth, ApiOperation, ApiTags} from "@nestjs/swagger";
import {RegisterDto} from "./dto/register.dto";
import {LoginDto} from "./dto/login.dto";
import {AuthService} from "./auth.service";
import {AuthGuard} from "@nestjs/passport";
import {GetUser} from "@app/common/decorations/getUser.drcorator";
import {UserDocument} from "@libs/db/models/user.model";
import {changeUserDto} from "./dto/change-user.dto";

@Controller('auth')
@ApiTags('用户')
export class AuthController {
  constructor(private readonly authService: AuthService) {
  }

  @Post('/registry')
  @ApiOperation({summary: '注册'})
  async register(@Body() body: RegisterDto) {
    const {username, password} = body
    return await this.authService.register({username, password})
  }

  @Post('/login')
  @UseGuards(AuthGuard('local'))
  @ApiOperation({summary: '登录'})
  async login(@Body() body: LoginDto, @GetUser() user: UserDocument) {
    return await this.authService.login(user)
  }

  @Get('/user')
  @UseGuards(AuthGuard('jwt'))
  @ApiBearerAuth()
  @ApiOperation({summary: '用户信息'})
  user(@GetUser() user: UserDocument) {
    return user
  }

  @Post("/user")
  @UseGuards(AuthGuard('jwt'))
  @ApiBearerAuth()
  @ApiOperation({summary: '修改用户信息'})
  async changeUser(@Body() userInfo: changeUserDto, @GetUser() user) {
    return await this.authService.changeUser(userInfo, user)
  }

  @Post('/logout')
  @UseGuards(AuthGuard('jwt'))
  @ApiBearerAuth()
  @ApiOperation({summary: '退出登录'})
  logout() {
    return {
      success: true
    }
  }


}
